/*
 *  $Id: TabCloser.cs 728 2009-07-19 16:35:05Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.Collections.Generic;

using AgateLib.Geometry;

namespace Ragima {
    public class TabCloser {
        private TabCloserRegion[] _regions;

        public TabCloser() {
            _regions = new TabCloserRegion[Constants.FieldSize];
            for (int i = 0; i < Constants.FieldSize; i++)
                _regions[i] = new TabCloserRegion(i);
        }

        public void Show(bool show) {
            for (int i = 0; i < Constants.FieldSize; i++)
                _regions[i].Show(show);
        }

        public IEnumerable<Region> GetRegions() {
            return _regions;
        }
    }

    public class TabCloserRegion: Region {
        private const int OverlayHeight = 140;
        private const int OverlayWidth = 107;
        private const int OverlayY = Constants.DisplayHeight - OverlayHeight;
        private const int OverlayDefaultY = OverlayY + OverlayHeight;
        private const int OverlayDelay = 5;
        private const int OverlayStep = 7;

        private int _delay, _wait, _offset;
        private bool _show;

        public TabCloserRegion(int i): base(new Rectangle(Constants.GetRowX(i)-2, OverlayDefaultY, OverlayWidth, OverlayHeight)) {
            _delay = i*OverlayDelay;
            _wait = _delay;
            _offset = OverlayHeight;
            _show = false;
        }

        public override void Draw() {
            if (_offset < OverlayHeight)
                Data.EnemyTurnOverlay.Draw(Rect.Location);
        }

        public override void OnProcess() {
            if (_show) {
                if (_wait > 0) {
                    _wait--;
                    return;
                }
                if (_offset == 0) return;
                _offset = Math.Max(_offset - OverlayStep, 0);
            } else {
                if (_wait < _delay) {
                    _wait++;
                    return;
                }
                if (_offset == OverlayHeight) return;
                _offset = Math.Min(_offset + OverlayStep, OverlayHeight);
            }
            Rectangle rect = Rect;
            rect.Y = OverlayY + _offset;
            Rect = rect;
        }

        public void Show(bool show) {
            _show = show;
        }
    }
}
